<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true,isEdit=false"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button size="small" type="success" @click="hAssign(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hClose(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="q.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="q.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="hSizeChange"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit?'编辑':'添加'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="cSubmit"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="cSubmit">取消</el-button>
          <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <el-dialog
      title="分配权限"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialogAssign"
      @close="showDialogAssign=false"
    >
      <assignPermission v-if="showDialogAssign" :id="id" @close="showDialogAssign=false" />
    </el-dialog>
  </div>
</template>

<script>
import { addRole, deleteRole, getRoles, updateRole } from '@/api/settings'
import assignPermission from './assignPermission'
export default {
  components: {
    assignPermission
  },
  data() {
    return {
      isEdit: false,
      q: { page: 1, pagesize: 2 },
      roles: [],
      total: 0,
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      showDialogAssign: false,
      id: ''
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async loadRoles() {
      try {
        const res = await getRoles(this.q)
        // console.log(res)
        this.roles = res.data.rows
        this.total = res.data.total
      } catch (e) {
        console.log(e)
      }
    },
    hSizeChange(val) {
      this.q.pagesize = val
      this.loadRoles()
    },
    hCurrentChange(val) {
      this.q.page = val
      this.loadRoles()
    },
    hClose(id) {
      // console.log(id)
      this.$confirm('真的要删除吗', '提示', { type: 'warning' }).then(async() => {
        try {
          const res = await deleteRole(id)
          // console.log(res.message)
          this.$message.success(res.message)
          if (this.roles.length === 1 && this.q.page > 1) this.q.page--
          this.loadRoles()
        } catch (e) { this.$message.error(e.message) }
      }).catch(() => {})
    },
    hSubmit() {
      this.$refs.roleForm.validate(async valid => {
        if (!valid) return
        let res = {}
        try {
          if (this.isEdit) { res = await updateRole(this.roleForm) } else {
            res = await addRole(this.roleForm)
          }
          this.$message.success(res.message)
          if (!this.isEdit) {
            if (this.total % this.q.pagesize === 0) this.total++
            this.q.page = Math.ceil(this.total / this.q.pagesize)
          }
          this.loadRoles()
          // this.$refs.roleForm.resetFields()
          this.roleForm = { }
          this.$refs.roleForm.clearValidate()
          this.showDialog = false
        } catch (e) { this.$message.error(e.message) }
      })
    },
    cSubmit() {
      this.showDialog = false
      this.roleForm = { name: '',
        description: '' }
      this.$refs.roleForm.clearValidate()
    },
    hEdit(row) {
      this.isEdit = true
      this.showDialog = true
      this.roleForm = { ...row }
    },
    hAssign(id) {
      this.showDialogAssign = true
      this.id = id
    }
  }
}
</script>
